﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using DAL;
using System.Data;

namespace BLL
{
    /// <summary>
    /// 业务父类(泛型约束，必须是类，必须有无参构造函数，子类只需继承而已)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseBLL<T> where T :class,new()
    {
        /// <summary>
        /// 数据访问类对象(由子类实例化)
        /// </summary>
        protected BaseDAL<T> dal = null;

        /// <summary>
        /// 抽象方法(由子类重写)
        /// </summary>
        protected abstract void SetDAL();

        /// <summary>
        /// 构造函数
        /// </summary>
        protected BaseBLL()
        {
            SetDAL();
        }

        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="isSuccess">执行是否成功(输出) true=成功 false=失败</param>
        /// <param name="primaryKeyValue">主键值</param>
        /// <returns>true=存在 false=不存在</returns>
        public virtual bool Exists(out bool isSuccess, object primaryKeyValue)
        {
            return dal.Exists(out isSuccess, primaryKeyValue);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="isSuccess">执行是否成功(输出) true=成功 false=失败</param>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数数组</param>
        /// <returns>true=存在 false=不存在</returns>
        public virtual bool Exists(out bool isSuccess, string strSql, params object[] paraList)
        {
            return dal.Exists(out isSuccess, strSql, paraList);
        } 
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>主键值 -1=出错</returns>
        public virtual int Add(T model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>true=成功 false=出错</returns>
        public virtual bool AddReturnBool(T model)
        {
            return dal.AddReturnBool(model);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Update(T model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="type">实体类</param>
        /// <param name="model">对象(包含主键值)</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Update(Type type, object model)
        {
            return dal.Update(type, model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="type">实体类</param>
        /// <param name="model">对象</param>
        /// <param name="primaryKeyValue">主键值</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Update(Type type, object model, object primaryKeyValue)
        {
            return dal.Update(type, model, primaryKeyValue);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKeyName">主键名</param>
        /// <param name="model">对象</param>
        /// <param name="primaryKeyValue">主键值</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Update(string tableName, string primaryKeyName, object model, object primaryKeyValue)
        {
            return dal.Update(tableName, primaryKeyName, model, primaryKeyValue);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model">主键值</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Delete(T model)
        {
            return dal.Delete(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKeyValue">主键值</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Delete(object primaryKeyValue)
        {
            return dal.Delete<T>(primaryKeyValue);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数数组</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual int Delete(string strSql, params object[] paraList)
        {
            return dal.Delete<T>(strSql, paraList);
        }
        #endregion

        #region 获取实体
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="isSuccess">执行是否成功(输出) true=成功 false=失败</param>
        /// <param name="primaryKeyValue">主键值</param>
        /// <returns>实体对象</returns>
        public virtual T GetModel(out bool isSuccess, object primaryKeyValue)
        {
            return dal.GetModel(out isSuccess, primaryKeyValue);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="isSuccess">执行是否成功(输出) true=成功 false=失败</param>
        /// <param name="strSql">sql语句或条件</param>
        /// <param name="paraList">where条件参数数组</param>
        /// <returns>实体对象</returns>
        public virtual T GetModel(out bool isSuccess, string strSql, params object[] paraList)
        {
            return dal.GetModel(out isSuccess, strSql, paraList);
        } 
        #endregion

        #region 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数数组</param>
        /// <returns>实体对象列表 null=出错</returns>
        public virtual List<T> GetList(string strSql, params object[] paraList)
        {
            return dal.GetList(strSql, paraList);
        }
        #endregion

        #region 获取分页列表
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="isSuccess">执行是否成功(输出) true=成功 false=失败</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="totalCount">总记录数(输出)</param>
        /// <param name="paraList">where条件参数数组</param>        
        /// <returns>实体对象列表 null=出错</returns>
        public virtual List<T> GetPagedList(uint pageIndex, ushort pageSize, string strSql, out uint totalCount, params object[] paraList)
        {
            return dal.GetPagedList(pageIndex, pageSize, strSql, out totalCount, paraList);
        }
        #endregion

        /// <summary>
        /// 获取首行首列值
        /// </summary>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数列表</param>
        /// <returns>实体对象</returns>
        public virtual T GetSingle(out bool isSuccess, string strSql, params object[] paraList)
        {
            return dal.GetSingle(out isSuccess, strSql, paraList);
        }

        #region 执行Sql语句
        /// <summary>
        /// 执行Sql
        /// </summary>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数列表</param>
        /// <returns>受影响的行数 -1=出错</returns>
        public virtual uint Execute(string strSql, params object[] paraList)
        {
            return dal.Execute(strSql, paraList);
        }

        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="strSql">sql语句或where语句</param>
        /// <param name="paraList">where条件参数列表</param>
        /// <returns>DataTable null=出错</returns>
        public virtual DataTable GetDataTable(string strSql, params object[] paraList)
        {
            return dal.GetDataTable(strSql, paraList);            
        }
        #endregion
    }
}
